#!/usr/bin/python

from UpCode import FileDataBase
from UpCode import color
import time
import os

class Cal_Data(FileDataBase):
    def __init__(self):
        FileDataBase.__init__(self)
        self.cx = FileDataBase.get_connect(self)[0]
        self.cu = FileDataBase.get_connect(self)[1]
        self.print_color = color("green")
        self.print_color_pur = color("blue")
        self.cu.execute("select * from type")
        print "...pro Initializing ..."
        time.sleep(0.3)
        print "Initializing ",
        time.sleep(0.3)
        print "...ok..."
        time.sleep(0.3)
        print "... Initialized ..."
        print "Start ...",
        time.sleep(0.3)
        print "ok"
        os.system("clear")
        res = self.cu.fetchall()
        res = map(self.print_color_pur.get_color,res)
        for item in res:
            print item
    def add_type(self,type_language):
        FileDataBase.insert_type(self,type_language)
        self.cu.execute("select * from type") 
        now =self.cu.fetchall()
        now = map(self.print_color.get_color,now)
        for item in now:
            print item
    def Clear_all_date(self):
        self.cu.execute("select * from type")
        some = self.cu.fetchall()
        for i in range(1,len(some)+1):
            self.cu.execute("delete from type where id=%d" % i)
            self.cx.commit()
        self.cu.execute("select * from type")
        list_type =  self.cu.fetchall()
        print list_type
        self.cu.close()
        self.cx.close()
        print "Ok submit"
        exit(0)

if __name__ == "__main__":
    proce = Cal_Data()
    
    print "input type to db :"
    type = 0
    while ( type != "q"):
        type = raw_input("type (Press q : quit / cl : clear ) : ")
        if (type == "clear"):
            proce.Clear_all_date()
        elif ( type != "q"):
            proce.add_type(type)
            print "ok submit"
        elif (type == "q"):
            proce.close()
            os.system("clear")
    exit(0)
    
